package com.orangelife.webservice.service.propfee.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.orangelife.common.AbstractV2TransferService;
import com.orangelife.exception.AppBizException;
import com.orangelife.framework.toolkit.validate.ValidateUtil;
import com.orangelife.service.PropFeeType.IPropFeeTypeService;
import com.orangelife.service.PropFeeType.model.PropFeeTypeDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.service.propfee.IPropFeeTypeWebService;

@Service("PropFeeTypeWebService")
public class PropFeeTypeWebServiceImpl extends AbstractV2TransferService
		implements IPropFeeTypeWebService {

	@Resource(name = "propFeeTypeService")
	private IPropFeeTypeService propFeeTypeService;

	@Override
	public Response getPropFeeTypeByFeeTypeId(
			HttpServletRequest servletRequest, String feeTypeId)
			throws Exception, AppBizException {
		/* feeTypeId为空的场合 */
		if (StringUtils.isBlank(feeTypeId)) {
			return this.abnormalResp(100002, "类型名称Id不能为空");

		}
		/* feeTypeId格式错误的场合 */
		if (!ValidateUtil.checkUUID(feeTypeId)) {
			logger.error(InfoConst.SYS_INFO_1003, "类型名称Id", feeTypeId);
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "类型名称Id", feeTypeId);
		}

		List<PropFeeTypeDto> propFeeTypeDtos = propFeeTypeService
				.selectByPrimaryKey(feeTypeId);
		return this.normalResp(0, 10, 1l, propFeeTypeDtos);

	}

	@Override
	public Response getPropFeeTypeByCommId(HttpServletRequest servletRequest,
			String commId) throws Exception, AppBizException {
		/* commID为空的场合 */
		if (StringUtils.isBlank(commId)) {
			return this.abnormalResp(100002, "小区ID不能为空");

		}
		/* commID格式错误的场合 */
		if (!ValidateUtil.checkUUID(commId)) {
			logger.error(InfoConst.SYS_INFO_1003, "小区ID", commId);
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "小区ID", commId);
		}

		List<PropFeeTypeDto> propFeeTypeDtos = propFeeTypeService
				.selectByCommID(commId);

		return this.normalResp(0, 10, 1l, propFeeTypeDtos);

	}

	@Override
	public Response deletePropFeeTypeByFeeTypeId(
			HttpServletRequest servletRequest, String feeTypeId)
			throws Exception, AppBizException {
		/* feeTypeId为空的场合 */
		if (StringUtils.isBlank(feeTypeId)) {
			return this.abnormalResp(100002, "feeTypeId不能为空");

		}

		/* feeTypeId格式错误的场合 */
		if (!ValidateUtil.checkUUID(feeTypeId)) {
			logger.error(InfoConst.SYS_INFO_1003, "类型名称Id", feeTypeId);
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "类型名称Id", feeTypeId);
		}

		propFeeTypeService.updateItemStat(feeTypeId);

		return this.normalResp();

	}

	@Override
	public Response insertPropFeeType(HttpServletRequest servletRequest,
			PropFeeTypeDto propFeeTypeDto) throws Exception, AppBizException {
		if (propFeeTypeDto == null) {
			return this.abnormalResp(100002, "参数不正确");
		}

		/* 各种参数为空的场合 */
		if (StringUtils.isBlank(propFeeTypeDto.getFeeTypeID())) {
			return this.abnormalResp(100002, "类型名称Id不能为空");
		}
		if (StringUtils.isBlank(propFeeTypeDto.getCommID())) {
			return this.abnormalResp(100002, "小区ID不能为空");
		}
		if (StringUtils.isBlank(propFeeTypeDto.getFeeTypeName())) {
			return this.abnormalResp(100002, "类型名称不能为空");
		}
		if (StringUtils.isBlank(propFeeTypeDto.getType())) {
			return this.abnormalResp(100002, "type不能为空");
		}
		if (StringUtils.isBlank(propFeeTypeDto.getCode())) {
			return this.abnormalResp(100002, "类型编码不能为空");
		}

		if (!StringUtils.isBlank(propFeeTypeDto.getItemStat())) {
			if (!propFeeTypeDto.getItemStat().equals("0")
					&& !propFeeTypeDto.getItemStat().equals("1")) {
				logger.error(InfoConst.SYS_INFO_1003, "状态",
						propFeeTypeDto.getItemStat());
				return this.abnormalResp(CodeConst.SYS_CODE_1003,
						InfoConst.SYS_INFO_1003, "状态",
						propFeeTypeDto.getItemStat());
			}
		}

		/* feeTypeId格式错误的场合 */
		if (!ValidateUtil.checkUUID(propFeeTypeDto.getFeeTypeID())) {
			logger.error(InfoConst.SYS_INFO_1003, "类型名称Id",
					propFeeTypeDto.getFeeTypeID());
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "类型名称Id",
					propFeeTypeDto.getFeeTypeID());
		}
		/* commID格式错误的场合 */
		if (!ValidateUtil.checkUUID(propFeeTypeDto.getCommID())) {
			logger.error(InfoConst.SYS_INFO_1003, "小区ID",
					propFeeTypeDto.getCommID());
			return this
					.abnormalResp(CodeConst.SYS_CODE_1003,
							InfoConst.SYS_INFO_1003, "小区ID",
							propFeeTypeDto.getCommID());
		}

		List<PropFeeTypeDto> temp = propFeeTypeService
				.selectByPrimaryKey(propFeeTypeDto.getFeeTypeID());
		if (temp != null && temp.size() > 0) {

			return this.abnormalResp(99, "信息已经存在");

		}

		propFeeTypeService.insertSelective(propFeeTypeDto);

		return this.normalResp();
	}

	@Override
	public Response updatePropFeeType(HttpServletRequest servletRequest,
			PropFeeTypeDto propFeeTypeDto) throws Exception, AppBizException {
		if (propFeeTypeDto == null) {
			return this.abnormalResp(100002, "参数不正确");
		}
		/* 各种参数为空的场合 */
		if (StringUtils.isBlank(propFeeTypeDto.getFeeTypeID())) {
			return this.abnormalResp(100002, "类型名称Id不能为空");
		}
		if (StringUtils.isBlank(propFeeTypeDto.getCommID())) {
			return this.abnormalResp(100002, "小区ID不能为空");
		}
		if (StringUtils.isBlank(propFeeTypeDto.getFeeTypeName())) {
			return this.abnormalResp(100002, "类型名称不能为空");
		}
		if (StringUtils.isBlank(propFeeTypeDto.getType())) {
			return this.abnormalResp(100002, "type不能为空");
		}
		if (StringUtils.isBlank(propFeeTypeDto.getCode())) {
			return this.abnormalResp(100002, "类型编码不能为空");
		}
		if (!StringUtils.isBlank(propFeeTypeDto.getItemStat())) {
			if (!propFeeTypeDto.getItemStat().equals("0")
					&& !propFeeTypeDto.getItemStat().equals("1")) {
				logger.error(InfoConst.SYS_INFO_1003, "状态",
						propFeeTypeDto.getItemStat());
				return this.abnormalResp(CodeConst.SYS_CODE_1003,
						InfoConst.SYS_INFO_1003, "状态",
						propFeeTypeDto.getItemStat());
			}
		}

		/* feeTypeId格式错误的场合 */
		if (!ValidateUtil.checkUUID(propFeeTypeDto.getFeeTypeID())) {
			logger.error(InfoConst.SYS_INFO_1003, "类型名称Id",
					propFeeTypeDto.getFeeTypeID());
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "类型名称Id",
					propFeeTypeDto.getFeeTypeID());
		}

		/* commID格式错误的场合 */
		if (!ValidateUtil.checkUUID(propFeeTypeDto.getCommID())) {
			logger.error(InfoConst.SYS_INFO_1003, "小区ID",
					propFeeTypeDto.getCommID());
			return this
					.abnormalResp(CodeConst.SYS_CODE_1003,
							InfoConst.SYS_INFO_1003, "小区ID",
							propFeeTypeDto.getCommID());
		}

		propFeeTypeService.updateByPrimaryKeySelective(propFeeTypeDto);

		return this.normalResp();
	}

	@Override
	public Response getPropFeeTypeByBean(HttpServletRequest servletRequest,
			PropFeeTypeDto propFeeTypeDto) throws Exception, AppBizException {
		if (propFeeTypeDto == null) {
			return this.abnormalResp(100002, "参数不正确");
		}

		if (!StringUtils.isBlank(propFeeTypeDto.getFeeTypeID())) {
			/* feeTypeId格式错误的场合 */
			if (!ValidateUtil.checkUUID(propFeeTypeDto.getFeeTypeID())) {
				logger.error(InfoConst.SYS_INFO_1003, "类型名称Id",
						propFeeTypeDto.getFeeTypeID());
				return this.abnormalResp(CodeConst.SYS_CODE_1003,
						InfoConst.SYS_INFO_1003, "类型名称Id",
						propFeeTypeDto.getFeeTypeID());
			}

		}
		if (!StringUtils.isBlank(propFeeTypeDto.getCommID())) {
			/* commID格式错误的场合 */
			if (!ValidateUtil.checkUUID(propFeeTypeDto.getCommID())) {
				logger.error(InfoConst.SYS_INFO_1003, "小区ID",
						propFeeTypeDto.getCommID());
				return this.abnormalResp(CodeConst.SYS_CODE_1003,
						InfoConst.SYS_INFO_1003, "小区ID",
						propFeeTypeDto.getCommID());
			}

		}

		List<PropFeeTypeDto> propFeeTypeDtos = propFeeTypeService
				.selectByPropFeeTypeDto(propFeeTypeDto);
		long total = 0;
		if (propFeeTypeDtos != null) {
			total = propFeeTypeDtos.size();
		}
		return this.normalResp(0, 1, total, propFeeTypeDtos);
	}

}
